package com.classic.okhttp.services;

import com.classic.okhttp.Cons;
import android.app.Activity;
import com.classic.okhttp.base.OkHttpUtils;
import com.classic.okhttp.zmoumall.utils.MD5;
import com.classic.okhttp.zmoumall.http.ACache;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.classic.okhttp.zmoumall.http.ObjectCallback;
import okhttp3.MediaType;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

import com.classic.okhttp.beans.HVProductHomePageBean;
import com.classic.okhttp.beans.HVProductInfoBean;
import com.classic.okhttp.beans.HVProductTypeInfoBean;


import com.classic.okhttp.enums.Enum;

public class IProductService
{

	private static String _getFullURL(String method)
	{
		return new StringBuffer( Cons.GATEWAY_URL ).append("product/").append(method).toString();
	}

	//获取产品主页
	public static void getProductHomePage(Activity activity, String cityCode, boolean isCache, ObjectCallback<HVProductHomePageBean> callback)
	{
		callback.setCallBackBean(new TypeToken<HVProductHomePageBean>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("cityCode", cityCode);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getProductHomePage");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(cityCode).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//获取产品列表
	public static void getProductList(Activity activity, String cityCode, Double lng, Double lat, String keyword, int pageindex, int pagesize, String projectId, String tagTitle, boolean isCache, ObjectCallback<ArrayList<HVProductInfoBean>> callback)
	{
		callback.setCallBackBean(new TypeToken<ArrayList<HVProductInfoBean>>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("cityCode", cityCode);
		params.put("lng", lng);
		params.put("lat", lat);
		params.put("keyword", keyword);
		params.put("pageindex", pageindex);
		params.put("pagesize", pagesize);
		params.put("projectId", projectId);
		params.put("tagTitle", tagTitle);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getProductList");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(cityCode).append(lng).append(lat).append(keyword).append(pageindex).append(pagesize).append(projectId).append(tagTitle).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}

	//获取产品规格列表
	public static void getProductTypeInfo(Activity activity, String productId, boolean isCache, ObjectCallback<HVProductTypeInfoBean> callback)
	{
		callback.setCallBackBean(new TypeToken<HVProductTypeInfoBean>(){}.getType());
		callback.setContext(activity);

		Map<String, Object> params = new HashMap<>();
		params.put("productId", productId);

		Gson gson=new Gson();
		String json=gson.toJson(params);

		String url = _getFullURL("getProductTypeInfo");

		if(isCache) {//添加缓存
			String key = MD5.MD5Encode(new StringBuffer(url).append(productId).toString());
			ACache aCache = ACache.get(activity);
			callback.setACache(aCache, key, isCache);
		}

		OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json;charset=utf-8"))
               	.url(url)
                .content(json)
                .addHeader("Token",Cons.mToken)
                .addHeader("DeviceID",Cons.mDeviceId)
				.addHeader("DeviceType",Cons.mDeviceType)
				.addHeader("Version",Cons.mVersion)
				.addHeader("VersionCode",Cons.mVersionCode)
				.addHeader("Data",Cons.getData(json))
				.addHeader("AppType",Cons.getAppType())
                .tag(activity)
                .build()
                .execute(callback);
	}
}
